package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public Result upload(List<String[]> list) {
//        上传后得结果信息
//        Map<String,Object> info=new HashMap<>();
        int okk=0;
        int err=0;
        int all=list.size();
        // 具体出错的信息集合
        List<String[]> errList = new ArrayList<>();
//        info.put("all",list.size());
        for (String[] strings : list) {
            String DateStr= strings[0];
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            Date parse = null;
            try {
                parse = sdf.parse(DateStr);
                OrderSetting orderSetting =new OrderSetting(parse,Integer.parseInt(strings[1]));
                orderSettingDao.add(orderSetting);
                okk++;
//                info.put("okk",info.get("okk")==null?1:Integer.parseInt(info.get("okk").toString())+1);
            } catch (Exception e) {
                e.printStackTrace();
                errList.add(strings);
                // 这里可以让他更新

                err++;
//                info.put("err",info.get("err")==null?1:Integer.parseInt(info.get("err").toString())+1);
            }

        }
//        info.put("errData",errList);
//        最后作为HTML显示到页面上
// <br>添加成功</div><ul><li>共8条数据</li><li>成功6条</li><li>失败2条</li><ul><li>2020-8-8 500</li></ul></ul>
        String errDate="";
        for (String[] strings : errList) {
            errDate+="<li>"+strings[0]+"   "+strings[1]+"</li>";
        }
        StringBuffer sb = new StringBuffer();
        sb.append("<br>添加成功</div><ul><li>共"+all+"条数据</li><li>成功"+okk+"条</li><li>失败"+err+"条</li><ul>"+errDate+"</ul></ul>");
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS,sb);
    }

    @Override
    public List<Map> getOrderSettingsByDate(String date) {
        List<Map> list = orderSettingDao.getOrderSettingsByDate(date);
        return list;
    }

    @Override
    public PageResult findData(String year, String month) {
        String start = year + "-" + month + "-" + "1";
        String end = year + "-" + month + "-" + "31";
        Page<OrderSetting> data = orderSettingDao.findData(start, end);
        for (OrderSetting datum : data) {
            int date = datum.getOrderDate().getDate();
//            datum.setDate(date);
        }
        return new PageResult(data.getTotal(), data.getResult());
    }

    @Override
    public void edit(OrderSetting orderSetting) {
        if (orderSetting.getNumber() == 0) {
            //如果可预约人数为0,直接从数据库中删除数据即可,否则在前端会显示为可预约0已预约0,不友好
            orderSettingDao.delete(orderSetting);
            return;
        }
//        先更新,如果返回1,表示有数据并且更新成功
        int update = orderSettingDao.editNumberByOrderDate(orderSetting);
        if (update != 1) {
//            没有更新,说明需要添加数据
            orderSettingDao.add(orderSetting);
        }
    }
}
